package mini.ideashare.cms.manager;

import mini.ideashare.cms.dao.UserDAO;
import mini.ideashare.cms.model.User;
import mini.ideashare.cms.model.qc.UserQC;
import mini.ideashare.cms.model.qc.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @Autowired
    private UserDAO userDAO;

    public List<User> queryUser(User user, Integer pageIndex , Integer pageSize){
        Page page = new Page(pageIndex,pageSize);
        UserQC qc  = new UserQC();
        qc.setPage(page);
        BeanUtils.copyProperties(user,qc);
        List<User> users = userDAO.listUser(qc);
        return users;
    }

    public Long countUser(User user){
        UserQC qc  = new UserQC();
        BeanUtils.copyProperties(user,qc);
        Long count = userDAO.countUser(qc);
        return count;
    }

    public int saveUser(User user){

        if(user.getId()!=null){
            return userDAO.updateUser(user);
        }else {
            user.setCreateTime(new Date());
            return userDAO.insertUser(user);
        }

    }
    public int deleteUser(Long id){

        return userDAO.deleteUserById(id);

    }



}
